package com.xie.smfs.mixin.client;

import com.xie.smfs.client.screen.TutorialScreen;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TexturedButtonWidget;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.Identifier;
import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket;
import net.minecraft.network.PacketByteBuf;
import io.netty.buffer.Unpooled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(InventoryScreen.class)
public abstract class InventoryScreenMixin extends HandledScreen<PlayerScreenHandler> {
    @Unique
    private static final Logger LOGGER = LoggerFactory.getLogger(InventoryScreenMixin.class);

    @Unique
    private static final Identifier OPEN_GHOST_SCREEN_PACKET = new Identifier("smfs", "open_ghost_screen");

    @Unique
    private static final Identifier TUTORIAL_BUTTON_TEXTURE = new Identifier("smfs", "textures/gui/tutorial_button.png");

    public InventoryScreenMixin(PlayerScreenHandler handler, PlayerInventory inventory, Text title) {
        super(handler, inventory, title);
    }

    @Inject(method = "init", at = @At("TAIL"))
    private void addGhostControlButton(CallbackInfo ci) {
        // 添加驭鬼按钮
        int ghostButtonX = this.x + 140;
        int ghostButtonY = this.y + 60;

        ButtonWidget ghostControlButton = ButtonWidget.builder(
                        Text.translatable("button.smfs.ghost_control"),
                        (button) -> {
                            LOGGER.info("点击了驭鬼按钮，开始尝试打开界面");
                            if (this.client != null && this.client.player != null) {
                                try {
                                    PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
                                    this.client.player.networkHandler.sendPacket(
                                            new CustomPayloadC2SPacket(OPEN_GHOST_SCREEN_PACKET, buf)
                                    );
                                    LOGGER.info("已发送打开界面的数据包请求");
                                } catch (Exception e) {
                                    LOGGER.error("发送数据包时发生异常: ", e);
                                }
                            } else {
                                LOGGER.warn("客户端或玩家对象为空，无法发送数据包");
                            }
                        }
                )
                .position(ghostButtonX, ghostButtonY)
                .size(25, 18)
                .build();

        this.addDrawableChild(ghostControlButton);

        // 添加人品纸按钮 - 使用 TexturedButtonWidget
        int tutorialButtonX = this.x + 190; // 右侧位置，避免重叠
        int tutorialButtonY = this.y + 60;

        TexturedButtonWidget tutorialButton = new TexturedButtonWidget(
                tutorialButtonX,
                tutorialButtonY,
                25,
                36,
                0, 0, // u, v - 默认状态纹理坐标
                1, // 纹理宽度
                TUTORIAL_BUTTON_TEXTURE,
                25, 36, // 纹理总宽度和高度
                (button) -> {
                    if (this.client != null && this.client.player != null) {
                        this.client.setScreen(new TutorialScreen());
                    }
                },
                Text.translatable("button.smfs.tutorial") // 悬停提示文本
        );

        this.addDrawableChild(tutorialButton);
    }
}